/**
 * 
 */
package com.whf15.o2o.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 作者 :whfo
 * @version 创建时间：2021年5月6日 下午8:13:17 类说明
 */
public class HttpServletRequestUtil {

	public static int getInt(HttpServletRequest request, String key) {
		try {
			// 从request参数中提取key并转换成 整形integer
			return Integer.decode(request.getParameter(key));
		} catch (Exception e) {
			// TODO: handle exception
			return -1;
		}
	}

	public static long getLong(HttpServletRequest request, String key) {
		try {
			// 从request参数中提取key并转换成 long
			return Long.valueOf(request.getParameter(key));
		} catch (Exception e) {
			// TODO: handle exception
			return -1l;
		}
	}

	public static double getDouble(HttpServletRequest request, String key) {
		try {
			// 从request参数中提取key并转换成 Double
			return Double.valueOf(request.getParameter(key));
		} catch (Exception e) {
			// TODO: handle exception
			return -1d;
		}
	}

	public static boolean getBoolean(HttpServletRequest request, String key) {
		try {
			// 从request参数中提取key并转换成 Boolean
			return Boolean.valueOf(request.getParameter(key));
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	/**
	 * 2021年5月6日
	 */
	public static String getString(HttpServletRequest request, String key) {
		try {
			String result = request.getParameter(key);
			if (result != null) {
				// result不为空，去掉两端的空格
				result = result.trim();
			}
			if ("".equals(result)) {
				// reuslt不为空
				result = null;
			}
			return result;
		} catch (Exception e) {
			return null;
		}
	}

}
